package views;

import DAO.AsignaturaDao;
import DAO.ProfesorDao;
import views.panels.PanelGeneral;
import java.awt.BorderLayout;
import java.util.Iterator;
import java.util.List;
import javax.swing.ImageIcon;
import models.Asignatura;
import models.Profesor;

/**
 *
 * @author vanessa
 */
public class InterfazConsultarCalificaciones extends javax.swing.JFrame {

    private ImageIcon icono;

    /** Creates new form InterfazConsultarCalificaciones */
    public InterfazConsultarCalificaciones() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
        icono = new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());
        inicializarComboBox();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        JSLogrosAsig = new javax.swing.JScrollPane();
        TLogro = new javax.swing.JTable();
        SPNotasEstu = new javax.swing.JScrollPane();
        TEstudiante = new javax.swing.JTable();
        BAceptar = new javax.swing.JButton();
        CBNombreAsignatura = new javax.swing.JComboBox();
        CBDocProfesor = new javax.swing.JComboBox();

        setTitle("Consultar Notas");
        setResizable(false);

        jLabel1.setText("Doc. Profesor");

        jLabel2.setText("Nombre de la asignatura:");

        TLogro.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nro. del Logro", "Descripción del Logro"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TLogro.setEnabled(false);
        JSLogrosAsig.setViewportView(TLogro);

        TEstudiante.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Nombre del Estudiante", "Nro. del Logro", "Calificación"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TEstudiante.setEnabled(false);
        SPNotasEstu.setViewportView(TEstudiante);

        BAceptar.setFont(new java.awt.Font("Tahoma", 1, 11));
        BAceptar.setText("Aceptar");
        BAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BAceptarActionPerformed(evt);
            }
        });

        CBNombreAsignatura.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));
        CBNombreAsignatura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBNombreAsignaturaActionPerformed(evt);
            }
        });

        CBDocProfesor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));
        CBDocProfesor.setEnabled(false);
        CBDocProfesor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBDocProfesorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(JSLogrosAsig, javax.swing.GroupLayout.DEFAULT_SIZE, 718, Short.MAX_VALUE)
                            .addComponent(SPNotasEstu, javax.swing.GroupLayout.DEFAULT_SIZE, 718, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(190, 190, 190)
                        .addComponent(BAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CBDocProfesor, 0, 543, Short.MAX_VALUE)
                            .addComponent(CBNombreAsignatura, 0, 543, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(CBNombreAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(CBDocProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addComponent(JSLogrosAsig, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(SPNotasEstu, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(BAceptar)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BAceptarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_BAceptarActionPerformed

    private void CBNombreAsignaturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBNombreAsignaturaActionPerformed
        if (CBNombreAsignatura.getSelectedIndex() > 0){
            AsignaturaDao asignaturaDao = new AsignaturaDao();
            Asignatura asignatura = asignaturaDao.encontrarAsignatura((String)CBNombreAsignatura.getSelectedItem());
            List profesores = asignatura.getProfesorList();
            CBDocProfesor.setEnabled(true);
            for (Iterator it = profesores.iterator(); it.hasNext();) {
                Profesor profesor = (Profesor) it.next();
                CBDocProfesor.addItem(String.valueOf(profesor.getDocprofesor()));
            }
        }
        else{
            CBDocProfesor.setSelectedIndex(0);
            CBDocProfesor.setEnabled(false);
        }
    }//GEN-LAST:event_CBNombreAsignaturaActionPerformed

    private void CBDocProfesorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBDocProfesorActionPerformed
        if (CBDocProfesor.getSelectedIndex() > 0){
            AsignaturaDao asignaturaDao = new AsignaturaDao();
            Asignatura asignatura = asignaturaDao.encontrarAsignatura((String)CBNombreAsignatura.getSelectedItem());
            ProfesorDao profesorDao = new ProfesorDao();
            Profesor profesor= profesorDao.encontrarProfesor(Long.parseLong((String)CBDocProfesor.getSelectedItem()));
            llenarTablas(asignatura, profesor);
        }
        else{
            borrarTablas();
        }
    }//GEN-LAST:event_CBDocProfesorActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazConsultarCalificaciones().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BAceptar;
    private javax.swing.JComboBox CBDocProfesor;
    private javax.swing.JComboBox CBNombreAsignatura;
    private javax.swing.JScrollPane JSLogrosAsig;
    private javax.swing.JScrollPane SPNotasEstu;
    private javax.swing.JTable TEstudiante;
    private javax.swing.JTable TLogro;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables

    private void inicializarComboBox() {
        AsignaturaDao asignaturaDao = new AsignaturaDao();
        List asignaturas = asignaturaDao.encontrarAsignaturas();
        for (Iterator it = asignaturas.iterator(); it.hasNext();) {
            Asignatura asignatura = (Asignatura) it.next();
            CBNombreAsignatura.addItem(asignatura.getNombreasignatura());
        }
    }

    private void borrarTablas() {
        int fila = 0;
        while (TEstudiante.getValueAt(fila, 0) != null){
            TEstudiante.setValueAt(null, fila, 0);
            TEstudiante.setValueAt(null, fila, 1);
            TEstudiante.setValueAt(null, fila, 2);
            fila++;
        }
        fila = 0;
        while (TLogro.getValueAt(fila, 0) != null){
            TLogro.setValueAt(null, fila, 0);
            TLogro.setValueAt(null, fila, 1);
            fila++;
        }
    }

    private void llenarTablas(Asignatura asignatura, Profesor profesor) {

    }
}
